
$(function(){
	shopCartIds = sessionStorage['shopCartIds'];
	total = sessionStorage['total'];
	productIds = sessionStorage['productIds'];
	$("#total").html(parseInt(total)+8);
	$("#amount").val(parseInt(total)+8);
	var ids = productIds.split(":");
	var quantity = "";
	for (i = 0; i < ids.length; i++) {
		if(ids[i]!=''){
		Newtouch.Ajax.sendParams({id:ids[i]},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/buyShopCart.jsp.findProductById",function(data){	
			$("#address").append('<li id="product_'+data.id+'"><img src="'+data.image+'" class="img-polaroid"/>'+data.name+'<span style="margin-left:120px">'+data.price+'</span>*<span id="count_'+data.id+'">'+sessionStorage[data.name]+'</span></li>');
			quantity=data.id+'='+sessionStorage[data.name]+':'+quantity;
		},function(err){
		});
		}
	}
	userName = sessionStorage['username'];
	$("#userName").val(userName);
	addressId=sessionStorage["addressId"];
	if(addressId!=null){
		Newtouch.Ajax.sendParams({addressId:addressId},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/buyShopCart.jsp.findAddressById",function(data){
			if(data == null){
				$("#userAddress").append('<button type="button" class="btn"  >添加地址</button>');
				
			}else{
				$("#userAddress").append("地址："+data.city_name+data.area_name+data.detail+data.deliveryPersonNm+data.deliveryMobile);
				$("#addressId").val(data.id);
			}
			
		},function(err){
		});
	}else{
	Newtouch.Ajax.sendParams({userId:sessionStorage['userId']},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/buyShopCart.jsp.findAddressByUserId",function(data){	
		if(data == null){
			$("#userAddress").append('<button type="button" class="btn"  >添加地址</button>');
			
		}else{
			$("#userAddress").append("地址："+data.city_name+data.area_name+data.detail+data.deliveryPersonNm+data.deliveryMobile);
			$("#addressId").val(data.id);
		}
		
	},function(err){
	});
	}
	$("#userAddress").bind("tap",function(){
		androidLocal({"type":4});
		androidLocal({"productIds":productIds});
		androidLocal({"shopCartIds":shopCartIds});
		androidLocal({"total":total});
		androidLocal({"comment":$("#comment").val()});
		window.location="chooseAddress.html";
	});
	
	//确认
	 $("#buyProduct").click(function(){
		    	var params = {};
		    	params['ids'] = $("#userName").val();
		    	params['productIds'] = productIds;
		    	params['addressId'] =$("#addressId").val();
		    	params['comment'] =$("#comment").val();
		    	params['amount'] =$("#amount").val();
		    	params['quantity'] = quantity;
		    	params['shopCartIds'] = shopCartIds;
		    	params['userId'] = sessionStorage['userId'];
		    	Newtouch.Ajax.sendParams(params,localStorage['APP_AJAX_PATH']+"/drinkingBar/view/buyShopCart.jsp.buyProduct",function(data){	
		    		 window.location="buyProductSuccess.html";
		    		
		    	},function(err){
		    	});
	 });	
});	    

var androidLocal=function(p){
	for(var k in p){
		sessionStorage[k]=p[k];
	}
	
}

	
	
